package com.pomr.model.sys;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;

import com.fasterxml.jackson.annotation.JsonFormat;


/**
 * User entity. @author MyEclipse Persistence Tools
 * 不使用懒加载  因为用户信息需要放到session中 如果使用使用懒加载则在获取session中信息的时候，get懒加载信息会报空指针错误，因为此时hibernate session早已经关闭，无法再次查询
 */
@Entity
@Table(name="user",uniqueConstraints = {@UniqueConstraint(columnNames={"uname"})})
public class User implements java.io.Serializable {
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getAgencyName() {
		return agencyName;
	}

	public void setAgencyName(String agencyName) {
		this.agencyName = agencyName;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}



	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Agency getAgency() {
		return agency;
	}

	public void setAgency(Agency agency) {
		this.agency = agency;
	}

	

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")//AUTO 在mysql中需要指定使用本地策略生成主键值
	private Integer id;
	@Column(name="name",length=50)
	private String name;//姓名
	@Column(name="password",length=20)
	private String password;//密码
	@Column(name="uname",length=20)
	private String uname;//用户名
	@Column(name="tel",length=20)
	private String tel;//联系电话
	@Column(name="addr",length=100)
	private String addr;//地址
	@Column(name="email",length=50)
	private String email;//邮箱
	@ManyToOne(fetch=FetchType.LAZY)// 指定多对一           关联机构表
	@JoinColumn(name="agency_id")
	private Agency agency;//如果是超级管理员为空
	
	@ManyToOne(fetch=FetchType.LAZY)// 指定多对一           关联角色
	@JoinColumn(name="type")
	private Role role;//1超级管理员 2机构管理员 3机构分级管理员 4普通用户
	@Formula("(select a.name from agency a where a.id=agency_id)")
	private String agencyName;//机构名
	@Formula("(select r.role_name from role r where r.id=type)")
	private String roleName;//角色名
	@Column(name="status",length=1,columnDefinition="varchar(1) default '0'")
	private String status;//状态 1：作废
	@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
	@Column(name="create_time")
	private Date createTime;//创建时间
	// Constructors

	/** default constructor */
	public User() {
	}

	/** minimal constructor */
	public User(String name, String password, String uname) {
		this.name = name;
		this.password = password;
		this.uname = uname;
		
	}

	/** full constructor */
	public User(String name, String password, String uname, String tel, String addr, String email) {
		this.name = name;
		this.password = password;
		this.uname = uname;
		this.tel = tel;
		this.addr = addr;
		this.email = email;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUname() {
		return this.uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getTel() {
		return this.tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getAddr() {
		return this.addr;
	}

	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}